const app = getApp();

Page({
  data: {
    hidden: false, //加载
    outline: false, //有无网络
    noData: false,
    state: "1",
    newsType: '1',
    userId:'',
    status: 'pay',
    list: []
  },
  onLoad(query) {
    // 页面加载


  },
  onReady() {
    // 页面加载完成
  },
  active: function(e) {
    var that = this;
    if (e.currentTarget.dataset.id != that.data.newsType) {
      that.setData({
        newsType: e.currentTarget.dataset.id,
        list: []
      })
      if (that.data.newsType == '1') {
        that.setData({
          state: '1',
        })
      } else if (that.data.newsType == '2') {
        that.setData({
          state: '2',
        })
      }
      that.post()
    }
  },
  chakan: function(e) {
    my.navigateTo({
      url: '/pages/wuliu/wuliu?data=' + JSON.stringify(e.currentTarget.dataset.id),
    })
  },

  onShow() {
    var that = this;
    my.getNetworkType({
      success: (res) => {
        if (res.networkAvailable) {
          that.setData({
            outline: false,
            list: [],
            currentPage: 1
          })
          // 获取token
          my.getStorage({
            key: 'token',
            success: function(res) {
              that.setData({
                token: res.data
              })
            },
            fail: function(res) {
              my.alert({ content: res.errorMessage });
            }
          })
          //获取useid
            my.getStorage({
            key: 'userId',
            success: function(res) {
              that.setData({
                userId: res.data
              })
            },
            fail: function(res) {
              my.alert({ content: res.errorMessage });
            }
          })
          that.post()
        } else {
          that.setData({
            outline: true
          })
        }
      }
    })
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
   /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.currentPage < this.data.paginationInfo.totalPage) {
      this.setData({
        currentPage: ++this.data.currentPage,
      })
    } else {
      this.setData({
        bottom: true
      })
    }
  },
  bindKeyInput(e) {
    this.setData({
      code: e.detail.value
    });
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: 'My App',
      desc: 'My App description',
      path: 'pages/index/index',
    };
  },
  post() {
    var that = this;
    that.setData({
      hidden: true,
    })
    var req = {
      token: that.data.token,
      userid: that.data.userId,
      state: that.data.state,
    }

    my.request({ // 调用自己的服务端接口，让服务端进行后端的授权认证，并且种session，需要解决跨域问题
      url: 'https://www.tiantong369.com/crm/usershoporderlist', // 该url是自己的服务地址
      method: 'POST',
      data: req,
      headers: {
        'x-access-token': that.data.token
      },
      success: (res) => {
        if (res.data.status == 200) {
          that.setData({
            hidden: false,
          })
          var arr = [];
          for (var i = 0; i < res.data.data.msg.length; i++) {
            arr.push(res.data.data.msg[i])
          }
          that.setData({
            list: arr,
            noData: false
          })
          if (that.data.list.length == 0) {
            that.setData({
              noData: true
            })
          }
        }else{
          my.showToast({
            content:res.data.message
          });
        }

      },
      fail: (res) => {
        console.log('/Wxpay_callback:', res)
      },
    });
  }
});
